/*
 * 文件名：MicroExamController.java
 * 版权：Copyright by www.infopub.top
 * 描述：
 * 修改人：wuzhiyang
 * 修改时间：2020年3月16日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.infopub.mgr.bpa.web;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.infopub.dto.bpa.BpaPublicProInfoDto;
import top.infopub.dto.bpa.MicroExamDto;
import top.infopub.dto.bpa.MicroExamQueryDto;
import top.infopub.enums.mkt.ProProcesssStatusEnum;
import top.infopub.mgr.bpa.jsonVo.MicroExamDetailJsonVo;
import top.infopub.mgr.bpa.service.BpaProcessManageService;
import top.infopub.mgr.bpa.service.MicroExamService;
import top.infopub.mgr.bpa.vo.MicroExamPo;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.mgr.mkt.service.ProjectInfoService;
import top.infopub.model.bpa.MicroExam;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;


/**商务事项
 * @author wuzhiyang
 * @version 2020年3月16日
 * @see MicroExamController
 * @since商务管理/商务事项/小微评审
 */
@Controller
@RequestMapping("/microExam")
public class MicroExamController extends GenericFlowController {
    @Autowired
    private BpaProcessManageService bpaProcessManageService;

    @Autowired
    private MicroExamService microExamService;
    @Autowired
    private ProjectInfoService projectInfoService;

    /**
     * 跳转列表页
     * @return
     */
    @RequestMapping("/list")
    public String list(HttpServletRequest request) {
        return "bpa/lay-microExam-list";
    }

    /**
     * 小微评审列表数据
     * @return
     */
    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<MicroExamPo> queryDataByCondition(MicroExamQueryDto microExamVo, ParamPage paramPage) {
        Page<MicroExamPo> page = new Page<MicroExamPo>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        // update by xieyong at 20210618 23:40
        page = microExamService.selectByDomainWhere(page, microExamVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    /**
     * 根据项目编号查询相关带入信息
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/getMicroExamByProCode")
    @ResponseBody
    public Map<String, Object> getMicroExamByProCode(HttpServletRequest request) {
        boolean flag = true;
        Map<String, Object> map = new HashMap<String, Object>();
        String proCodeAndCodes = request.getParameter("proCodeAndCodes");
        String[] proCodes = proCodeAndCodes.split("\\_");
        try {
            //带出项目合同的信息
            BpaPublicProInfoDto proContractVo = projectInfoService.queryBpaProInfoByProCode(proCodes[1]);
            if (proContractVo != null) {
                map.put("microExamProCode", proContractVo);
            }
            else {
                flag = false;
            }
        }
        catch (Exception e) {
            log.error("查询项目合同记录异常", e);
            flag = false;
        }
        map.put("flag", flag);
        return map;
    }

    /** 
     * 小微评审-新增
     * 0=保存，1=提交
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping("/save")
    @ResponseBody
    public RspVo save(String bodyStr, Integer submitType) {
        String str = StringEscapeUtils.unescapeJava(bodyStr);
        MicroExamDetailJsonVo jsonVo= JSONObject.parseObject(str,MicroExamDetailJsonVo.class);
        RspVo rspVo= microExamService.save(jsonVo,submitType);
        return rspVo;
    }

    /**
     * 根据id查询小微评审详情  > 注意：方法命名必须为：toDealOrDetail
     * @param id 小微评审Id
     * @return
     * @see
     */
    @RequestMapping("/toDealOrDetail")
    public String toDealOrDetail(HttpServletRequest request, String id, String proCode) {
        request.setAttribute("id", id);
        MicroExam microExam = microExamService.selectByPrimaryKey(id);
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        request.setAttribute("isAuditFlag", "1");
        request.setAttribute("auditTabFlag", "0");//登录人和审批人一致使用该字段是否显示审核信息
        if (microExam != null) {
            if (org.apache.commons.lang3.StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), microExam.getProcessStatus())
                    || org.apache.commons.lang3.StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS9.getCode(), microExam.getProcessStatus())) {
                request.setAttribute("isAuditFlag", "0");
            }
            if (org.apache.commons.lang3.StringUtils.isNotBlank(microExam.getProcessId()) && microExam.getProcessId().length() > 10) {
                request.setAttribute("processId", microExam.getProcessId());
                String auditTabFlag = bpaProcessManageService.checkShowAuditTab(microExam.getProcessId(), vo);
                if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), microExam.getProcessStatus())
                        && StringUtils.equals("0", auditTabFlag)) {
                    request.setAttribute("isAuditFlag", "0");
                }
                request.setAttribute("auditTabFlag", auditTabFlag);
            }
        }
        request.setAttribute("proCode", proCode);
        return "bpa/lay-MicroReview-detail";
    }

    @RequestMapping("/queryDetail")
    @ResponseBody
    public RspVo queryDetail(String id) {
        MicroExamDetailJsonVo detailJson = microExamService.getDetail(id);
        return RspVo.getSuccessResponse(detailJson);
    }

    //根据procode获取项目的基本信息
    @RequestMapping("/queryProjDetail")
    @ResponseBody
    public RspVo queryProjDetail(String proCode) {
        MicroExamDetailJsonVo micExamJson = new MicroExamDetailJsonVo();
        //项目的基础信息
        BpaPublicProInfoDto dto = projectInfoService.queryBpaProInfoByProCode(proCode);
        MicroExamDto info = new MicroExamDto();
        BeanUtils.copyProperties(dto, info);
        micExamJson.setInfo(info);
        return RspVo.getSuccessResponse(micExamJson);
    }
}
